/*-
 * Ring.java
 *
 * Copyright (c) 2007, Stein Gunnar Bakkeby
 * All rights reserved.
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of Stein Gunnar Bakkeby nor the names of its
 *       contributors may be used to endorse or promote products derived
 *       from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS "AS IS" AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package com.googlecode.attention.at;

import static com.googlecode.attention.ComLogStatements.*;

/**
 * AT command that represents the fact that the connection has been reset. This
 * class is package protected, mainly because it should not be possible to
 * send such an event manually. Note that this is not a standard AT command,
 * and it is likely to be a signal used by the RS232 implementation, where when
 * the connection is broken or initialised, the value 0xFFFFFFFF is received.
 * This command object is meant to be used as a signal rather than an actual
 * command. 
 *
 * @author Stein Gunnar Bakkeby
 */
class ConnectionReset extends AtCommand<ConnectionReset> {

  /**
   * Creates the connection reset signal, the internal AT command string will
   * be set to "CONNECTION_RESET".
   */
  public ConnectionReset() {
    super(null);
    setCommand("CONNECTION_RESET", Command.READ);
  }
  
  public ConnectionReset(final AtListener<ConnectionReset> listener) {
    super(listener);
  }
  
  /**
   * This will parse the incoming response/signal.
   * 
   * @param signal The incoming response/signal to parse.
   */
  @Override
  public void parse(final String signal) {
    COM_DEBUG.log("Parsing incoming CONNECTION_RESET AT command");
    parseSuccess(signal);
    notifyListeners();
  }
  
  /**
   * This will parse the incoming response/signal and set the internal
   * success variable to true or false.
   * 
   * @param signal The incoming response/signal to parse.
   */
  @Override
  protected void parseSuccess(final String signal) {
    success = (signal.charAt(0) == 0xFFFFFFFF || signal.charAt(0) == 0x0);
  }
  
  protected ConnectionReset getThis() {
    return this;
  }
}
